package newmeeting;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class InvitesListPanel extends JPanel implements ActionListener{
	
	private DefaultListModel listModel;
	private DefaultListModel listModel2;
	private JList invitesJList;
	private JList invitedJList;
	private JScrollPane scrollPanel;
	private JScrollPane scrollPanel2;
	private JButton inviteButton;
	private JButton unInviteButton;
	
	public static void main (String args[]){
		JFrame frame = new JFrame("Invites");
		InvitesListPanel panel = new InvitesListPanel();
		DefaultListModel model = panel.getModel();

		model.addElement(new Person("Ola", "dumdum"));
		model.addElement(new Person("Line", "tamtam"));
		model.addElement(new Person("Gustav", "hahaha"));
		
		frame.getContentPane().add(panel);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public InvitesListPanel(){
		super();
		listModel = new DefaultListModel();
		listModel2 = new DefaultListModel();
		
		GridBagLayout layout = new GridBagLayout();
		setLayout(layout);
		GridBagConstraints GridBagItem = new GridBagConstraints();
		GridBagItem.insets = new Insets(2, 10, 2, 1);
		GridBagItem.fill = GridBagConstraints.VERTICAL;
		
		invitesJList = new JList(getModel());
		invitesJList.setName("InvitesList");
		invitesJList.setCellRenderer(new PersonRenderer());
		invitesJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPanel = new JScrollPane(invitesJList);
		GridBagItem.gridx = 0;
		GridBagItem.gridy = 0;
		scrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		add(scrollPanel);
		
		unInviteButton = new JButton();
		GridBagItem.gridx = 0;
		GridBagItem.gridy = 2;
		unInviteButton.setName("UnInviteButton");
		unInviteButton.setText("Remove");
		unInviteButton.addActionListener(this);
		add(unInviteButton);
		
		inviteButton = new JButton();
		GridBagItem.gridx = 0;
		GridBagItem.gridy = 3;
		inviteButton.setName("InviteButton");
		inviteButton.setText("Invite");
		inviteButton.addActionListener(this);
		add(inviteButton);
		
		invitedJList = new JList(getModel2());
		invitedJList.setName("InvitedList");
		invitedJList.setCellRenderer(new PersonRenderer());
		invitedJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPanel2 = new JScrollPane(invitedJList);
		GridBagItem.gridx = 0;
		GridBagItem.gridy = 4;
		scrollPanel2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		add(scrollPanel2);
		
		setModel(listModel);
		setModel2(listModel2);
	}
	
	public void setModel(DefaultListModel lModel){
		this.listModel = lModel;
		invitesJList.setModel(lModel);
	}
	
	public void setModel2(DefaultListModel lModel2){
		this.listModel2 = lModel2;
		invitedJList.setModel(lModel2);
	}
	
	public DefaultListModel getModel(){
		return this.listModel;
	}
	
	public DefaultListModel getModel2(){
		return this.listModel2;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource().equals(inviteButton)) {
			if (invitesJList.getSelectedValue() != null) {
				Person newPerson = (Person) invitesJList.getSelectedValue();
				listModel2.add(0, newPerson);
				((DefaultListModel) invitesJList.getModel()).remove(invitesJList.getSelectedIndex());
				invitesJList.setSelectedIndex(0);
				
			}
			else if (invitesJList.getSelectedValue() == null) {
				System.out.println("please select a person");
				invitesJList.setSelectedIndex(0);
			}
		}
		else if (arg0.getSource().equals(unInviteButton)) {
			if (invitedJList.getSelectedValue() != null) {
				Person newPerson = (Person) invitedJList.getSelectedValue();
				listModel.add(0, newPerson);
				((DefaultListModel) invitedJList.getModel()).remove(invitedJList.getSelectedIndex());
				invitedJList.setSelectedIndex(0);
			}
			else if (invitedJList.getSelectedValue() == null) {
				System.out.println("please select a person");
				invitedJList.setSelectedIndex(0);
			}
		}
	}
}
